package com.leps.nq.bu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leps.nq.bu.entity.BuCust;
import com.leps.nq.bu.mapper.*;
import com.leps.nq.bu.service.IBuCustService;
import com.leps.nq.bu.vo.CustQueryModel;
import com.leps.nq.bu.vo.CustVo;
import com.leps.nq.common.util.SecurityUtil;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.op.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@Service
public class BuCustServiceImpl extends ServiceImpl<BuCustMapper, BuCust> implements IBuCustService {

    @Autowired
    private BuCustMapper buCustMapper;
    @Autowired
    private BuAreaMapper buAreaMapper;
    @Autowired
    private BuStationMapper buStationMapper;
    @Autowired
    private BuPlcMapper buPlcMapper;
    @Autowired
    private BuPumpMapper buPumpMapper;
    @Autowired
    private BuWellMapper buWellMapper;
    @Autowired
    private OpReplenishMapper replenishMapper;
    @Autowired
    private OpAlarmMapper alarmMapper;
    @Autowired
    private OpInspectionMapper inspectionMapper;
    @Autowired
    private OpRetireMapper retireMapper;
    @Autowired
    private OpWaterTestMapper waterTestMapper;
    @Autowired
    private OpBubbleReplenishMapper bubbleReplenishMapper;
    @Autowired
    private OpDeviceMaintenanceMapper deviceMaintenanceMapper;

    @Override
    public PageVo<CustVo> pageByQueryModel(CustQueryModel custQueryModel) {
        Page<CustVo> page = Page.of(custQueryModel.getPageNum(), custQueryModel.getPageSize());
        page = this.buCustMapper.selectByQueryModel(page, custQueryModel);
        return PageVo.of(page);
    }

    @Override
    @Transactional
    public boolean updateCascade(BuCust cust) {
        // 补填数据
        cust.setUpdateBy(SecurityUtil.getUserName());
        cust.setUpdateTime(new Date());
        // get origin cust name
        BuCust origin = this.buCustMapper.selectById(cust.getCustId());
        final String originName = origin.getCustName();
        // update cust
        this.buCustMapper.updateById(cust);
        // update cascade cust name of child tables if necessary
        final int custId = cust.getCustId();
        final String newCustName = cust.getCustName();
        if (Objects.equals(originName, newCustName)) return true;
        this.buAreaMapper.updateCustName(newCustName, custId);
        this.buStationMapper.updateCustName(newCustName, custId);
        this.buPlcMapper.updateCustName(newCustName, custId);
        this.buPumpMapper.updateCustName(newCustName, custId);
        this.buWellMapper.updateCustName(newCustName, custId);
        this.alarmMapper.updateProperty("cust_name", newCustName, "cust_id", custId);
        this.bubbleReplenishMapper.updateProperty("cust_name", newCustName, "cust_id", custId);
        this.inspectionMapper.updateProperty("cust_name", newCustName, "cust_id", custId);
        this.replenishMapper.updateProperty("cust_name", newCustName, "cust_id", custId);
        this.retireMapper.updateProperty("cust_name", newCustName, "cust_id", custId);
        this.waterTestMapper.updateProperty("cust_name", newCustName, "cust_id", custId);
        return true;
    }

    @Override
    public List<BuCust> listForSelect() {
        if(SecurityUtil.isAdmin()) {
            return this.list();
        }
        long userId = SecurityUtil.getUserId();
        return this.buCustMapper.selectListByUserId(userId);
    }
}
